﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using spbtzb;
using System.Data.OleDb;

public partial class MessageBoard_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.lbCurPage.Text = "1";
            Bind_Msg();
        }
    }

    protected void Bind_Msg()
    {
        String strCount = "0";
        String strCmd = "SELECT * FROM MsgBrd where Status = true order by CreateTime desc";
        DataTable dt = AccessHelper.ExecuteDataTable(spbtzb.ConString.ConnString, CommandType.Text, strCmd);
        if (dt == null || dt.Rows.Count == 0)
        {
            return;
        }

        this.lbl_Count.Text = dt.Rows.Count.ToString();
        dvbtn.Visible = true;
        PagedDataSource ps = new PagedDataSource();
        ps.AllowPaging = true;
        ps.PageSize = 15;
        ps.CurrentPageIndex = Convert.ToInt32(this.lbCurPage.Text) - 1;
        ps.DataSource = dt.DefaultView;
        rp_Msgs.DataSource = ps;
        this.btnPreviousPage.Enabled = true;
        this.btnNextPage.Enabled = true;
        if (ps.CurrentPageIndex == 0)
        {
            this.btnPreviousPage.Enabled = false;
        }
        if (ps.CurrentPageIndex == ps.PageCount - 1)
        {
            this.btnNextPage.Enabled = false;
        }
        this.rp_Msgs.DataBind();
    }
    protected void btok_Click(object sender, EventArgs e)
    {
        String strUserName = "统战网用户";
        if (this.cb_IsAnony.Checked != true && this.tb_UserName.Text.Trim() != "")
        {
            strUserName = this.tb_UserName.Text.Trim();
        }

        String strContent = AuthenCtr.Encode(this.tb_Content.Text.Trim());
        string sqlCmd = "INSERT INTO MsgBrd (CreateTime, UserName, IP, Content, Status) VALUES(@CreateTime,@UserName,@IP,@Content, @Status)";
        OleDbParameter[] pars = new OleDbParameter[5];
        pars[0] = AccessHelper.MakeInParam(@"CreateTime", System.Data.OleDb.OleDbType.Date, 8, DateTime.Now);
        pars[1] = AccessHelper.MakeInParam(@"UserName", AccessHelper.CheckForNullString(strUserName));
        pars[2] = AccessHelper.MakeInParam(@"IP", AccessHelper.CheckForNullString(GetClientIP()));
        pars[3] = AccessHelper.MakeInParam(@"Content", AccessHelper.CheckForNullString(strContent));
        pars[4] = AccessHelper.MakeInParam(@"Status", System.Data.OleDb.OleDbType.Boolean, 1, false);

        int iRet = AccessHelper.ExecuteNonQuery(spbtzb.ConString.ConnString, System.Data.CommandType.Text, sqlCmd, pars);
        if (iRet > 0)
        {
            //Response.Write("<script>alert('文章添加成功！');</script>");
            this.divAlert.InnerHtml = "留言提交成功，正在等待审核...";
            Response.Write("<script>window.location.href=window.location.href;</script>");//刷新

        }
        else
        {
            // 跳转到错误处理页面
            Response.Redirect("~/Common/ErrorPage.aspx?ErrorMessage=" + "添加留言失败，请重新操作或联系管理员!");
        }
    }

    //注意ISA防火墙设置问题   
    private string GetClientIP()
    {
        string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.UserHostAddress;
        }
        return result;
    }


    protected void btnNextPage_Click(object sender, System.EventArgs e)
    {
        this.lbCurPage.Text = Convert.ToString(Convert.ToInt32(this.lbCurPage.Text) + 1);
        this.Bind_Msg();
    }

    protected void btnPreviousPage_Click(object sender, System.EventArgs e)
    {
        this.lbCurPage.Text = Convert.ToString(Convert.ToInt32(this.lbCurPage.Text) - 1);
        this.Bind_Msg();
    }
}

